<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="value"
        v-globalImageError="defaultImg"
        :src="value"
        class="avatar"
        @click.stop.prevent="isShow = true"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除图片按钮 -->
    <div v-if="value" class="delImg" @click="delImg">X</div>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" width="500px" :visible.sync="isShow">
      <img style="width: 100%" :src="value">
    </el-dialog>
    <!-- 进度条显示 -->
    <transition name="fade">
      <el-progress
        v-if="loading"
        :text-inside="true"
        :stroke-width="24"
        :percentage="percentage"
        status="success"
      />
    </transition>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDCBxVpulNmwvgCUiX6QWNiphKhofJirkT',
  SecretKey: 'RdZFgrmSt44pLcdQzJOFpAF4ey2rQjmo'
})

export default {
  name: 'UploadImage',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // imageUrl: '',
      isShow: false,
      percentage: 0,
      loading: false,
      defaultImg: require('@/assets/common/bigUserHeader.png')
    }
  },
  methods: {
    // 上传前的验证,返回boolean值,true开始上传,false中止上传
    beforeAvatarUpload(file) {
      const isPic = file.type === 'image/png' || file.type === 'image/jpeg'
      if (!isPic) {
        this.$message.error('请上传png或jpeg图片')
        return false
      }
      return true
    },
    // 自定义上传
    httpRequest(res, file) {
      // console.log(res, file)
      this.loading = true
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'hr77-1308947992' /* 必须 存储桶名*/,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: res.file.uid + '' /* 必须 要求为字符串格式*/,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          // 上传进度
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              this.loading = false
            }
          }
        },
        // 这里必须使用箭头函数,this指向问题,用普通函数this为当前调用的函数
        (err, data) => {
          console.log(err || data)
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    // 删除图片
    delImg() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  position: relative;
  width: 178px;
  &:hover {
    .delImg {
      display: block;
    }
  }
  .delImg {
    display: none;
    position: absolute;
    top: 0px;
    right: 10px;
    font-size: 20px;
    cursor: pointer;
  }
  .fade-enter-active {
    transition: all 0.05s;
  }
  .fade-enter {
    opacity: 0;
  }
  .fade-leave {
    opacity: 0;
  }
  .fade-leave-active {
    transition: all 0.05s;
  }
}
</style>
